#include "Image.h"

using namespace YXX_AS;

Image::Image(const std::filesystem::path& path, bool flip /*= false*/)
{
	Init(path, flip);
}

bool Image::Init(const std::filesystem::path& path, bool flip /*= false*/)
{
	int w, h, c;

	stbi_set_flip_vertically_on_load(static_cast<int>(flip));

	auto ext = path.extension();
	if (ext == "hdr")
	{

	}
	else
	{
		auto stbi_data = stbi_load(path.string().c_str(), &w, &h, &c, 0);
		if (!stbi_data)
			return false;

		width = static_cast<size_t>(w);
		height = static_cast<size_t>(h);
		channel = static_cast<size_t>(c);
		data = new float[width * height * channel];

		for (size_t i = 0; i < width * height * channel; i++)
			data[i] = static_cast<float>(stbi_data[i]) / 255.f;

		stbi_image_free(stbi_data);
	}
	return true;
}
